package android.coocoo.views;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.coocoo.common.CategoriesListAdapter;
import android.coocoo.common.Const;
import android.coocoo.models.CategoryInfo;
import android.coocoo.models.Settings;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class DisplaySettingInfoActivity extends ListActivity{
	Resources res;
	Settings settings;
	ArrayList<CategoryInfo> categoriesList;
	
	CheckBox cbTurnOff;
	CheckBox cbMute;
	ImageView btAddCategory;
	ImageView btSaveSettings;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_setting_info);
		settings = (Settings)getIntent().getParcelableExtra("settings");
		categoriesList = getIntent().getParcelableArrayListExtra("categoriesList");
		res = getResources();
		
		cbMute = (CheckBox)findViewById(R.id.cbMute);
		cbMute.setChecked(settings.isMute());
		
		cbTurnOff = (CheckBox)findViewById(R.id.cbTurnOff);
		cbTurnOff.setChecked(settings.isTurnOff());
		cbTurnOff.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
				if (isChecked){
					System.out.println("Turned off");
					cbMute.setEnabled(false);
				}
				else{
					System.out.println("Turned on");
					cbMute.setEnabled(true);
				}
			}
		});
		
        this.categoriesList = getIntent().getParcelableArrayListExtra("categoriesList");
//        Log.e("pass_flag", "Intent created");
        setListAdapter(new CategoriesListAdapter(this, categoriesList));
        
        btAddCategory = (ImageView)findViewById(R.id.btAddCategory);
        btAddCategory.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				CategoryInfo category = new CategoryInfo();
				Intent intent = new Intent(DisplaySettingInfoActivity.this, DisplayCategoryInfoActivity.class);
				intent.putExtra("category", category);
				startActivityForResult(intent, Const.Request.ADD_CATEGORY);
			}
		});
        
        btSaveSettings = (ImageView)findViewById(R.id.btSaveSettings);
        btSaveSettings.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				System.out.println("SaveSetting pressed");
				Intent intent = new Intent();
				setResult(Const.Result.SUCCESS, intent);
				intent.putParcelableArrayListExtra("returnCategoriesList", categoriesList);
				intent.putExtra("returnSettings", settings);
				finish();
			}
        });
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		System.out.println("Result code = " + resultCode);
		if (requestCode == Const.Request.ADD_CATEGORY){
			if (resultCode == Const.Result.SUCCESS){
				CategoryInfo category = (CategoryInfo)intent.getParcelableExtra("returnCategory");
				categoriesList.add(category);
				setListAdapter(new CategoriesListAdapter(DisplaySettingInfoActivity.this, categoriesList));
			}else{
				
			}
		}
	}
}
